package me.qingji.zebra;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import me.qingji.zebra.bean.Article;
import me.qingji.zebra.bean.Link;
import me.qingji.zebra.core.ArticleCrawler;
import me.qingji.zebra.core.sync.ArticleSync;
import me.qingji.zebra.parser.ArticleParser;
import me.qingji.zebra.parser.ChapterParser;
import me.qingji.zebra.parser.TextParser;
import me.qingji.zebra.parser.impl.LiAoArticleParser;
import me.qingji.zebra.parser.impl.LiAoChapterParser;
import me.qingji.zebra.parser.impl.LiAoTextParser;

public class TianYaBookCityCrawler {

	static HashSet<Link> waitiingList = new HashSet<Link>();

	public static void main(String[] args) throws IOException {
		String contentsUrl = "http://www.tianyabook.com/liao/index.htm";

		Fetcher fetcher = new Fetcher();
		fetcher.setContentType("text/html; charset=gb2312");
		fetcher.setCharset("gb2312");

		// 采集、解析文章列表
		String listHtml = fetcher.doGet(contentsUrl);
		ArticleParser contentParser = new LiAoArticleParser();
		Set<Link> articleList = contentParser.parse(listHtml, contentsUrl);
		waitiingList.addAll(articleList);
		// Link("李敖快意恩仇录","http://www.tianyabook.com/liao/laky/index.html");
		int count = 3;
		if (waitiingList.size() > 0 && count >= 0) {
			@SuppressWarnings("unchecked")
			HashSet<Link> ss = (HashSet<Link>) waitiingList.clone();
			waitiingList.clear();
			for (Link link : ss) {
				load(fetcher, link);
			}
			count--;
		}
	}

	public static void load(Fetcher fetcher, Link link) {
		try {
			ChapterParser chapterParser = new LiAoChapterParser();
			TextParser textParser = new LiAoTextParser();

			ArticleCrawler crawler = new ArticleCrawler(chapterParser,
					textParser);
			Article article = crawler.crawl(fetcher, link);
			ArticleSync sync = new ArticleSync("c:\\ebook\\");
			sync.sync(article);
		} catch (Exception e) {
			waitiingList.add(link);
			System.out.println("采集失败:" + link);
			e.printStackTrace();
		}
	}
}
